<template>
  <el-card class="m-8">

    <section>

      <section slot="header" class="mb-4 flex items-center">

        <p class="ml-10">参数名称： </p>
        <el-input v-model="dataForm.keyword" placeholder="请输入用户昵称"></el-input>

        <p class="ml-10">状态：</p>
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option
            v-for="item in statusOptions"
            :key="item.val"
            :label="item.name"
            :value="item.val"
          >
          </el-option>
        </el-select>

        <el-button class="mr-4" type="primary" @click="dataForm.page=1;initData()">搜索</el-button>

      </section>

      <!--      <section slot="header" class="mt-10 flex items-center">-->
      <!--        <el-button class="mr-4" type="primary" @click="addIndepUser">新增</el-button>-->
      <!--      </section>-->

      <el-table :data="tableList" align="center" border class="mt-4" stripe>
        <el-table-column label="参数名称" prop="name"/>
        <el-table-column label="描述" prop="desc"/>
        <el-table-column label="配置" prop="val"/>
        <el-table-column label="状态" prop="status">
          <template slot-scope="{row}">
            <span v-if="row.status===1">启用</span>
            <span v-else>禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作人" prop="admin.admin_name"/>
        <el-table-column label="操作时间" prop="updated_at"/>
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <el-button plain type="primary" @click="addIndepUser(row)">编辑</el-button>
            <!--            <el-popconfirm-->
            <!--              cancel-button-text='不用了'-->
            <!--              confirm-button-text='好的'-->
            <!--              icon="el-icon-info"-->
            <!--              icon-color="red"-->
            <!--              title="确定删除吗？"-->
            <!--              @confirm="delRow(row)"-->
            <!--            >-->
            <!--              <el-button slot="reference" type="danger">删除</el-button>-->
            <!--            </el-popconfirm>-->

          </template>
        </el-table-column>
      </el-table>

      <cus-pagination
        :limit.sync="dataForm.per_page"
        :page.sync="dataForm.page"
        :total="total"
        @pagination="initData"
      />

    </section>


    <el-dialog v-dialogDrag :title="!isEdit?'新增':'编辑'" :visible.sync="dialogFormVisible"
               @close="handleCloseDialog('addForm')"
    >
      <el-form ref="addForm" :model="dynamicValidateForm" :rules="rules">

        <el-form-item
          label="参数名称"
          prop="name"
        >
          <el-input v-model="dynamicValidateForm.name"></el-input>
        </el-form-item>

        <el-form-item
          label="参数描述"
          prop="desc"
        >
          <el-input v-model="dynamicValidateForm.desc"></el-input>
        </el-form-item>

        <el-form-item
          label="参数配置"
          prop="val"
        >
          <el-input v-model="dynamicValidateForm.val"></el-input>
        </el-form-item>


        <el-form-item
          label="状态"
          prop="status"
        >
          <el-radio-group v-model="dynamicValidateForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="handleAddUser('addForm')">保存</el-button>
          <el-button type="danger" @click="dialogFormVisible = false">取消</el-button>
        </span>
    </el-dialog>


  </el-card>
</template>

<script>
import { EditArgs, giftList } from '../../api/guardActivity/argsConfig'

export default {
  data() {
    return {
      rowId: '',
      tempOptions: [],
      multipleOptions: [
        { value: 1, label: '1倍' },
        { value: 10, label: '10倍' },
        { value: 30, label: '30倍' },
        { value: 50, label: '50倍' }
      ],
      options: [],
      gameId: '',
      hasGame: false,
      isEdit: false,
      dialogPoolVisible: false,
      detailFormVisible: false,
      setStatus: 0,
      dialogFormVisible: false,
      dynamicValidateForm: {
        name: '',
        desc: '',
        val: ''
      },
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        desc: [{ required: true, message: '请输入', trigger: 'blur' }],
        val: [{ required: true, message: '请输入', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      },
      times: [],
      inputVal: 0,
      outputVal: 0,
      radio: 1,
      min: '',
      max: '',
      statusOptions: [
        { val: -1, name: '全部' },
        { val: 0, name: '禁用' },
        { val: 1, name: '启用' }
      ],

      setDialogVisible: false,
      tableList: [],
      dateList: undefined,
      dataForm: {
        keyword: '',
        status: -1,
        page: 1,
        per_page: 10
      },
      meta: {
        page: 1,
        perPage: 10
      },
      total: 1,
      id: '',
      // 拒绝原因
      setting: {
        balance_coin: 0
      },
      sysInstanceIndex: 1,
      sysInstanceTotalPages: 10
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      try {
        const { data } = await giftList(this.dataForm)
        this.tableList = data.data
        this.dataForm.page = data.current_page
        this.total = data.total
      } catch (e) {
        console.error(e)
      }
    },
    handleCloseDialog(formName) {
      this.sysInstanceIndex = 1
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    },
    handleAddUser(val) {
      this.$refs[val].validate(valid => {
        if (valid) {
          let obj = {}
          for (let key in this.dynamicValidateForm) {
            if (this.dynamicValidateForm[key] !== undefined && this.dynamicValidateForm[key] !== '') {
              obj[key] = this.dynamicValidateForm[key]
            }
          }
          obj.app_id = this.gameId

          obj.id = this.rowId
          EditArgs(obj).then(res => {
            if (res.code === 200) {
              this.initData()
              this.dialogFormVisible = false
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              })
            }
          })

        } else {
          return false
        }
      })
    },
    addIndepUser(row) {
      if (row.id) {
        this.rowId = row.id
        this.isEdit = true
        //todo 改成行内信息
        this.dynamicValidateForm = {
          desc: row.desc,
          name: row.name,
          val: row.val,
          status: row.status
        }
      } else {
        this.dynamicValidateForm = {
          name: '',
          desc: '',
          val: '',
          status: ''
        }
        this.rowId = ''
        this.isEdit = false
      }
      this.dialogFormVisible = true
    },
    handleReset() {
      this.dataForm = {
        keyword: '',
        status: -1,
        page: 1,
        per_page: 10
      }
    }

  }
}
</script>

<style scoped>
.el-input {
  width: 200px;
}

/deep/ .el-table .cell {
  flex-wrap: wrap;
}
</style>
